home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48hor1 / symbmat.src < prev    next >
Text File  |  1990-10-18  |  4KB  |  169 lines

  1. %%HP: T(3)A(R)F(.);
  2. DIR
  3. @       SYMBOLIC MATRICES by Eliel Louzoun
  4. @       translated to HP 48 by Per Stenius, Helsinki Univ. of Tech.
  5. @       -----------------
  6. @ This is a set of programs which handle symbolic matrices for the hp48sx.
  7. @ The matrices are entered as a list, for example {{A B }{ C D }}. This package
  8. @ contains programs for determinant,inverse of matrices,eigen values,
  9. @ multiplication of matrices & multiplication by a scalar.
  10. @ Bugs: The eigenvalue program works only if the values are all real.
  11.  
  12. det
  13. \<< DUP SIZE \-> A B
  14.     \<<
  15.        IF B 1 ==                   @ det of a 1x1 matrix
  16.        THEN A 1 GET 1 GET
  17.        ELSE
  18.          IF B 2 ==
  19.          THEN A DET2               @ call basic case of 2x2 determinant
  20.          ELSE 0
  21.           1 B  FOR I
  22.              A 1 GET I GET
  23.              \-> E \<< IF E 0 SAME THEN ELSE
  24.              A 1  I MINOR det E    @ recursive call
  25.              * -1 1 I + ^ * +
  26.           END \>> 
  27.           NEXT
  28.          END
  29.        END
  30.      \>>
  31.   \>>
  32.             @ This is an accessory program to compute A*D-B*C
  33. DET2        @ from {{A B}{C D}}
  34. \<<  \-> A
  35.     \<< A 1 GET 1 GET
  36.         A 2 GET 2 GET *
  37.         A 1 GET 2 GET
  38.         A 2 GET 1 GET * -
  39.     \>>
  40.  \>>
  41.  
  42.              @ This is also an accessory program to compute M(i,j) of
  43. MINOR        @ an element in the matrix
  44. \<< \-> A B C
  45.   \<< A SIZE \-> S
  46.      \<< 1 S FOR I
  47.           IF B I \=/ THEN A I GET
  48.        \-> D
  49.      \<< 1 S FOR J
  50.            IF J C \=/ THEN D J GET
  51.            END NEXT
  52.            S 1 - \->LIST
  53.      \>>
  54.       END NEXT S 1 - \->LIST
  55.     \>>
  56.   \>>
  57. \>>
  58.  
  59.            @ Compute the inverse of the matrix in level 1.
  60.            @ The  output is 2: B (a matrix)
  61.            @                1: C (an algebraic expression)
  62.            @ where inv(A) = B / C .
  63.            @ This program use the cramer rule to compute the inverse.
  64. inv
  65. \<<  DUP SIZE \-> A B
  66.   \<< 1 B FOR I
  67.        1 B FOR J
  68.          A J I MINOR det
  69.          -1 I J + ^ *
  70.        NEXT B \->LIST
  71.      NEXT B \->LIST
  72.      A det
  73.   \>>
  74. \>>
  75.  
  76.             @ matrices multiplication
  77.             @ input 2: A
  78.             @       1: B
  79.             @ output 1: A*B
  80. MMUL
  81. \<< \-> A B
  82.   \<< A SIZE B SIZE B 1 GET SIZE
  83.      \-> L C R
  84.     \<< 1 L FOR I A I GET
  85.        \-> LI
  86.        \<< 1 R FOR J 0
  87.             1 C FOR K LI K GET
  88.                  B K GET J GET
  89.                  * +
  90.             NEXT
  91.           NEXT R \->LIST
  92.         \>>
  93.         NEXT L \->LIST
  94.      \>>
  95.    \>>
  96. \>>
  97.  
  98.              @ scalar multiplication program
  99.              @ input  2: scalar
  100.              @        1: matrix
  101.              @ output 1: matrix = scalar * matrix
  102. SCMUL 
  103. \<< DUP SIZE \-> M A S
  104.   \<< 1 S FOR I A I GET \-> C
  105.     \<< 1 S FOR J C J GET M *
  106.        NEXT
  107.     \>> S \->LIST
  108.     NEXT S \->LIST
  109.   \>>
  110. \>>
  111.  
  112.             @ eliminate one real root from polynom
  113.             @ input  3: a polynom in S [P(S)]
  114.             @        2: the degree of this polynom
  115.             @        1: the root we want to eliminate [a]
  116.             @ output 1: P(S)/(S-a)
  117. DIVP
  118. \<< \-> P K R
  119.   \<< P 'S' R - / 'S' K 1 - TAYLR \>>
  120. \>>
  121.  
  122. eigf        @ This program returns the eigen function of a given matrix
  123. \<< DUP SIZE \-> A B
  124.   \<< B '-S' SIDN A
  125.      add det
  126.   \>>
  127. \>>
  128.  
  129.            @ sum of two matrices
  130.            @ input  2: A (matrix)
  131.            @        1: B (matrix)
  132.            @ output 1: A + B
  133. add
  134. \<<  DUP SIZE \-> B A S
  135.   \<< 1 S FOR I A I GET B I GET
  136.      \-> C D
  137.       \<< 1 S FOR J C J GET
  138.              D J GET +
  139.          NEXT
  140.       \>> S \->LIST
  141.      NEXT S \->LIST
  142.   \>>
  143. \>>
  144.  
  145.             @ This program return an identity matrix multiplied by a scalar
  146.             @ input  1: size (scalar)
  147.             @        2: const (algbraic object)
  148.             @ output 1: const * I
  149. SIDN
  150. \<< \-> K S
  151.   \<< 1 K FOR I
  152.        1 K FOR J
  153.           I J == S *
  154.        NEXT K \->LIST
  155.      NEXT K \->LIST
  156.   \>>
  157. \>>
  158.  
  159. EIGV        @ returns the eigenvalues of a matrix
  160. \<< \-> A
  161.   \<< A eigf STEQ A SIZE
  162.      1 FOR I RCEQ 'S' 0 ROOT
  163.          RCEQ I 3 PICK DIVP
  164.          STEQ
  165.      -1 STEP { S EQ } PURGE
  166.   \>>
  167. \>>
  168. END
  169.